package cn.uce.cc.srv.biz.impl;

import cn.uce.base.PubApiObjectConstants;
import cn.uce.base.ccos.MsgCRMParam;
import cn.uce.base.ccos.WebPoint;
import cn.uce.base.page.Page;
import cn.uce.base.page.Pagination;
import cn.uce.base.vo.IcsOrgVo;
import cn.uce.cc.api.service.biz.ICcosOrgBiz;
import cn.uce.cc.srv.client.CcosInterfaceClient;
import cn.uce.cc.srv.utils.HttpClientPubApiUtils;
import cn.uce.common.constants.PubApiURLConstants;
import cn.uce.utils.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;




@Service("ccosOrgBiz")
public class CcosOrgBiz implements ICcosOrgBiz {
    private static final Logger log = LoggerFactory.getLogger(CcosOrgBiz.class);

    @Resource
    CcosInterfaceClient ccosInterfaceClient;
    @Resource
    private PubApiURLConstants pubApiURLConstants;



    @Override
    public Pagination<WebPoint> getWebPointsForWorkOrder(Map<String, String> map) {
        Pagination<WebPoint> resultList = new Pagination<>();
        Map<String, Object> page_map = new HashMap();
        Map paramMap = new HashMap<String, String>();
        paramMap.put("deptCode", String.valueOf(map.get("keyWorkds")));
        paramMap.put("currentPage", String.valueOf(map.get("currentPage")));
        paramMap.put("pageSize", "10");
        paramMap.put("compCode", "yimidida");
        paramMap.put("deptTypes", "0,1,2,3,4,5,6,7");
        paramMap.put("status", "1");
        String result =null;
        try {
            log.warn("getWebPointsForWorkOrder,begin"+JSON.toJSONString(map));
             result = ccosInterfaceClient.getWebPointsForWorkOrder(String.valueOf(map.get("keyWorkds")),String.valueOf(map.get("currentPage")));
            log.warn("getWebPointsForWorkOrder,end"+result);
            if (StringUtil.isNotBlank(result)){
                JSONObject jSONObject = JSONObject.parseObject(result);
                String str_api_temp = jSONObject.getString("data");
                JSONObject jSONObject_temp = JSONObject.parseObject(str_api_temp);
                int totalRecord = jSONObject_temp.getInteger("totalRecord");
                int currentPage = jSONObject_temp.getInteger("currentPage");
                List webpointList = JSONObject.parseArray(jSONObject_temp.getString("records"));
                List<WebPoint> webppoints = new ArrayList<WebPoint>();
                if (webpointList != null) {
                    for (Object obj : webpointList) {
                        WebPoint webPoint = new WebPoint();
                        String str_temp = obj.toString();
                        JSONObject jsonObject_obj = JSONObject.parseObject(str_temp);
                        webPoint.setDeptCode(jsonObject_obj.getString("deptCode"));
                        webPoint.setDeptName(jsonObject_obj.getString("deptName"));
                        webPoint.setDeptType(jsonObject_obj.getString("deptType"));
                        webPoint.setCompCode(jsonObject_obj.getString("compCode"));
                        if (!"yimidida".equals(webPoint.getDeptCode()) && !"yimidida".equals(webPoint.getCompCode()) && !"壹米滴答供应链".equals(webPoint.getDeptName())) {
                            webppoints.add(webPoint);
                        }
                    }
                }
                Page page = new Page();
                page.setTotal(totalRecord);
                page.setCurrentPage(currentPage);
                resultList.setPage(page);
                resultList.setData(webppoints);
            }
            log.warn("getWebPointsForWorkOrder"+JSON.toJSONString(resultList));
        }catch (Exception e){
            log.error("getWebPointsForWorkOrder",e);
        }
        return resultList;
    }


    @Override
    public MsgCRMParam getNetDetial(String deptCode)   {
        String result = "";
        try {
            log.warn("getNetDetial,begin"+JSON.toJSONString(deptCode));
            result = ccosInterfaceClient.getNetDetial(deptCode);
            log.warn("getNetDetial,end"+result);
//            result = HttpclientTools.doGet(pubApiURLConstants.getDetialQueryByDeptcode(), paramMap, null);
            if (StringUtil.isEmpty(result)){
                return null;
            }
            JSONObject jSONObject = JSONObject.parseObject(result);
            String str_api_temp = jSONObject.getString("data");
            JSONObject netDetial = JSONObject.parseObject(str_api_temp);
            MsgCRMParam msgCRMParam = new MsgCRMParam();
            msgCRMParam.setDeptCode(netDetial.getString("deptCode"));
            msgCRMParam.setDeptName(netDetial.getString("deptName"));
            msgCRMParam.setDetailAddress(netDetial.getString("detailAddress"));
            msgCRMParam.setMobile(netDetial.getString("contactPhone"));
            msgCRMParam.setServicePhone(netDetial.getString("servicePhone"));
            log.warn("getNetDetial,return"+msgCRMParam);
            return msgCRMParam;
        } catch (Exception e) {
            log.error("getNetDetial出现异常：" + e);
        }
        return null;
    }

    /**
     * 根据网点编码获取网点信息
     * @Author liupan
     * @Description
     * @Date  2020-07-01 14:08
     */
    @Override
    public String getNetWorkInFo(String deptCode) {
        String result = "";
        Map map = new HashMap<String, Object>();
        map.put("deptCode", deptCode);
        String msg_temp = HttpClientPubApiUtils.buildUpStructure(PubApiObjectConstants.NETWORK_QUERY, map);
        try {
            log.warn("getNetWorkInFo,begin"+JSON.toJSONString(deptCode));
            result = ccosInterfaceClient.getNetWorkInFo(msg_temp);
            log.warn("getNetWorkInFo,end"+result);
        } catch (Exception e) {
            log.error("getNetWorkInFo出现异常：" + e);
        }
        return result;
    }

    @Override
    public String getWebPoints(Map<String, String> map) {
        Map<String, Object> page_map = new HashMap();
        String webpoints = "";
        // String url_param = "?deptCode=" + String.valueOf(map.get("keyWorkds")) +
        // "&currentPage=" + String.valueOf(map.get("currentPage"));
        try {
            Map paramMap = new HashMap<String, String>();
            paramMap.put("deptCode", String.valueOf(map.get("keyWorkds")));
            paramMap.put("currentPage", String.valueOf(map.get("currentPage")));
            log.warn("getWebPoints,begin"+JSON.toJSONString(map));
            webpoints = ccosInterfaceClient.getComplaintNetWorkByKeyWords(map);
            log.warn("getWebPoints,end"+webpoints);
        } catch (Exception e) {
            log.error("getComplaintNetWorkByKeyWords出现异常：" + e);
        }
        JSONObject jSONObject = JSONObject.parseObject(webpoints);
        String str_api_temp = jSONObject.getString("data");
        JSONObject jSONObject_temp = JSONObject.parseObject(str_api_temp);
        int totalPage = jSONObject_temp.getInteger("totalPage");
        int totalRecord = jSONObject_temp.getInteger("totalRecord");
        int currentPage = jSONObject_temp.getInteger("currentPage");
        int recordEnd = jSONObject_temp.getInteger("recordEnd");
        int recordStart = jSONObject_temp.getInteger("recordStart");
        List webpointList = JSONObject.parseArray(jSONObject_temp.getString("records"));
        List<WebPoint> webppoints = new ArrayList<WebPoint>();
        if (webpointList != null) {
            for (Object obj : webpointList) {
                WebPoint webPoint = new WebPoint();
                String str_temp = obj.toString();
                JSONObject jsonObject_obj = JSONObject.parseObject(str_temp);
                webPoint.setDeptCode(jsonObject_obj.getString("deptCode"));
                webPoint.setDeptName(jsonObject_obj.getString("deptName"));
                webppoints.add(webPoint);
            }
        }
        page_map.put("webppoints", webppoints);
        page_map.put("totalRecord", totalRecord);
        page_map.put("recordStart", recordStart);
        page_map.put("recordEnd", recordEnd);
        return JSON.toJSONString(page_map);
    }


}